import math


def new_cent(X):
	x = 0
	y = 0
	for i in range(len(X)):
		x += X[i][0]
		y += X[i][1]
	return [round(x / len(X), 1), round(y / len(X), 1)]


cent = [[3, 2], [9, 5], [2, 6]]
data = [[3, 2], [3, 9], [8, 6], [9, 5], [2, 4], [3, 10], [2, 6], [9, 6], [2, 2]]
data_label = ['A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3']
cent_new = []
sum = 0
while cent_new != cent:
	sum += 1
	A = []
	B = []
	C = []
	AA = []
	BB = []
	CC = []
	cent_new = cent
	print("循环第", sum, "次的三类簇心为：", cent)
	for i in data:
		l = data_label[data.index(i)]
		print(l, "与A,B,C之间的距离（曼哈顿）：")
		arr = []
		for j in cent:
			a = abs(i[0]-j[0])+abs(i[1]-j[1])
			arr.append(round(a, 1))
		print(arr)
		b = arr.index(min(arr)) + 1
		if b == 1:
			A.append(i)
		elif b == 2:
			B.append(i)
		elif b == 3:
			C.append(i)
	# print(b)
	cent = [new_cent(A), new_cent(B), new_cent(C)]
	for z in A:
		z1 = data_label[data.index(z)]
		AA.append(z1)
	for x in B:
		x1 = data_label[data.index(x)]
		BB.append(x1)
	for y in C:
		y1 = data_label[data.index(y)]
		CC.append(y1)
	print("循环第", sum, "次的分类结果：")
	print(AA, "\n", BB, "\n", CC)
	print("#" * 80)

